// pages/group/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    page: 1,
    user_id:0,
    type:'houses',
    loading: 0,
    lastpage: 0,
    groupData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    var window = app.globalData.window;
    window.windowHeight = window.windowHeight - 30;
    this.setData({ window: window, id: options.id ? options.id : 0, type: options.type })
   // this.setData({ window: window, id: 91 })
    app.getUserInfo((userInfo)=>{
      this.data.userInfo = userInfo;
      console.log(userInfo);
      this.getGroupList();
      this.getTopicList();
    })
  },

  selectGroup:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    this.setData({id:id});
    wx.setNavigationBarTitle({
      title: e.currentTarget.dataset.title,
    })
    this.getTopicList(1);
  },
 
  //
  getGroupList: function () {
    var that = this;
    var type = this.data.type;
    app.api("sns/group/get", {
      type: type,
      status:1,
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length>0){
        var groupData = [{
          id: 0,
          group_name: '全部',
        }].concat(res.data.list);
        that.setData({ groupData: groupData });
      }
      
    })
  },


  //api evens/list 活动列表 
  getTopicList: function (reset = 0) {
    var that = this;
    //重新加载 
    if (reset == 1) {
      that.setData({ page: 1, listData: [], loading: 0, lastpage: 0 });
      app.setPageData("sns/group/index", { reload: 0 });
    }
    if (this.data.loading == 1 || this.data.lastpage == 1) {
      return false;
    }
    this.data.loading = 1;
    //   lastpage:0,
    var page = that.data.page;
    //console.log(event_type);
    var p = {
      page: page,
      //status: 1,
      store_id: this.data.userInfo.community ? this.data.userInfo.community.store_id : 0,
      type: this.data.type,
      order: 'id desc',
      limit: 15,
    };
    if (this.data.id ){
      p.pid = this.data.id;
    }
   
    app.api("sns/topic/list",p, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length == 0) {
        that.data.lastpage = 1;
      }
      page++;
      that.setData({ page: page, listData: that.data.listData.concat(res.data.list) });

    })
  },
  thumb_up: function (e) {//列表点赞 
    var that = this;
    var index = e.currentTarget.dataset.id;
    var topic_id = e.currentTarget.dataset.topic_id;
    app.thumb_up(topic_id, function (res) {
      var topicData = that.data.listData[index];
      if (res.type == "add") {
        topicData.good_count++;
        topicData.thumb_up_ed = 1;
      } else if (res.type == "del") {
        topicData.good_count--;
        topicData.thumb_up_ed = 2;
      }
      that.data.listData[index] = topicData;
      that.setData({
        listData: that.data.listData
      });
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var reload = app.getPageData("group/index", 'reload');
    if (reload == 1) {
      this.getTopicList(1);
      this.getGroupList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },


  moreAct: function () {
    var that = this;
    var itemListArray = [];
   
    if (this.data.userInfo.tenement_type.company || this.data.userInfo.committee  ) { //管理员
      if (this.data.id){
        itemListArray.push({
          text: '编辑此圈', act:function(){
            wx.navigateTo({
              url: '/pages/group/add?id=' + this.data.id,
            })
        }});
        itemListArray.push({ text: '删除此圈', act: this.delGroup });
      }
      itemListArray.push({
        text: '创建圈子', act: function () {
          wx.navigateTo({
            url: '/pages/group/add?type=' +"houses",
          })
        }
      });

    }
    itemListArray.push({
      text: '回到首页', act: function () {
        wx.switchTab({ url: '/pages/index/index' })
      }
    });

  

    var itemList = [];
    //console.log(itemListArray);
    for (var i in itemListArray) {
      itemList.push(itemListArray[i].text);
    }
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        console.log(itemListArray[res.tapIndex]);
        itemListArray[res.tapIndex]['act']();
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  delGroup:function(){
    var id = this.data.id;
    var that =this;
    wx.showModal({
      title: '删除',
      content: '确认删除吗？',
      success: function (res) {
        if (res.confirm) {
          app.api("sns/group/del", { id: id }, function (res) {
            if (res.code != 200) {
              app.alert(res.msg);
              return false;
            }
            app.alert("操作成功");
            that.setData({id:0})
            that.onPullDownRefresh();
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  //下拉刷新
  onPullDownRefresh: function () {
    this.getTopicList(1);
    this.getGroupList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getTopicList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  scrolltolower: function (e) {
    console.log(e);
  },

  apply1: function () {
    wx.navigateTo({
      url: '/pages/group/issue?pid=' + this.data.id+"&type="+this.data.type
    })
  },
  thumb_list_up: function (e) {//列表点赞 
   var that = this;
   var index = e.currentTarget.dataset.id;
   var topic_id = e.currentTarget.dataset.topic_id;
   app.thumb_up(topic_id, function (res) {
     var topicData = that.data.listData[index];
     if (res.type == "add") {
       topicData.good_count++;
       topicData.thumb_up_ed = 1;
     } else if (res.type == "del") {
       topicData.good_count--;
       topicData.thumb_up_ed = 2;
     }
     that.data.listData[index] = topicData;
     that.setData({
       listData: that.data.listData
     });
   })
   // console.log(topicData);
 },
  //浏览图片
  previewListImage: function (e) {
    var current = e.target.dataset.src;
    var index = e.target.dataset.index;
    var pics = this.data.listData[index].pics;
    wx.previewImage({
      current: current,
      urls: pics
    })
  },
})